'''
Created on 13/lug/2012

@author: esgrano
'''
from host import host
import utility
import threading
from subprocess import call
import time

class command(threading.Thread):
    '''
    define a command
    '''

    __name = None
    __description = None
    __arguments = []
    __host = host()
    __libexec = None
    __retCode = None
    __formatted = None
    __contacts = None
    __nomeThread = None
    __smsCommand = None
    
    __maxCheck = 0
    __maxCheckTime = 0

    def __init__(self):
        threading.Thread.__init__(self)
        self.__nomeThread = threading.Thread.getName(self)
        
    def setContacts(self,cont):
        self.__contacts = cont

    def setHost(self,host):
        self.__host.setHostName(host.getHostName())
        self.__host.setIP(host.getIP())
        
    def setSMSComand(self,smscom):
        self.__smsCommand = smscom
        
    def getHost(self):
        return self.__host
    
    def setName(self,name):
        self.__name = name
    
    def getName(self):
        return self.__name
        
    def setDescription(self,description):
        self.__description = description
    
    def getDescription(self):
        return self.__description
    
    def getArguments(self):
        return self.__arguments
        
    def setArguments(self,arguments):
        self.__arguments = arguments

    def __checkNrpe(self,args):
        strNrpe = 'check_nrpe -H ' + (self.getHost()).getHostName() + ' -c ' + args;
        return strNrpe
         
    def __format(self):
        " format "
        
        cmdType = self.getName()
        _arguments = self.getArguments()
        if cmdType == 'check_nrpe':
            _myCommand = self.__checkNrpe(_arguments)
        else:
            _myCommand = self.__libexec + utility.PATH_SEPARATOR + cmdType + " " + _arguments
            
        return _myCommand

    def setStatus(self, returnCode):
        self.__retCode = returnCode
        
    def getStatus(self):
        return self.__retCode
    
    def setFormatted(self,commandFormatted):
        self.__formatted = commandFormatted

    def getFormatted(self):
        return self.__formatted

    def setMaxChecks(self,nCheck):
        self.__maxCheck = nCheck
        
    def getMaxChecks(self):
        return self.__maxCheck
    
    def setMaxCheckTime(self,checkTime):
        self.__maxCheckTime = checkTime

    def getMaxCheckTime(self):
        return self.__maxCheckTime
    
    def setLibexec(self,libexec):
        self.__libexec = libexec
    
    def alert(self, contacts):
        h = self.getHost()
        if self.getStatus() == utility.NAGIOS_CRITICAL:
        #                try:
            utility.sendSMS(self.__smsCommand,h.getHostName(),self.getDescription(),contacts)
        #                except Exception as e:
        #                   raise Exception(e.strerror)

       
    def run(self):
        """ Execute a nagios command
        """
        cmdFormatted = self.__format();
        __ret = None
        
        nMaxCritical = 0
        nMaxOK = 0
        nMaxWarning = 0
        nMaxUnknowkn = 0
            
        try:
            nCheck = self.getMaxChecks();
            
            for n in range(0,int(nCheck)):
                #ret=call(cmdFormatted,stdout=None,stderr=None)
                print (str(n) + ") Lancio comando (" + (self.__host).getHostName() + ": " + cmdFormatted)
                ret=2
                if ret == utility.NAGIOS_CRITICAL:                
                    nMaxCritical = nMaxCritical + 1
                elif ret == utility.NAGIOS_OK:
                    nMaxOK = nMaxOK+ 1
                elif ret == utility.NAGIOS_WARNING:
                    nMaxWarning = nMaxWarning +1
                else:
                    nMaxUnknowkn = nMaxUnknowkn + 1
                
                time.sleep(int(self.__maxCheckTime) * 60)
                #utility.delay(int(self.__maxCheckTime) * 60)
                
            if (int(nMaxCritical) == int(nCheck)):
                self.setStatus(utility.NAGIOS_CRITICAL)
            elif (int(nMaxOK) == int(nCheck)):
                self.setStatus(utility.NAGIOS_OK)
            elif (int(nMaxWarning) == int(nCheck)):
                self.setStatus(utility.NAGIOS_WARNING)
            else:
                self.setStatus(utility.NAGIOS_UNKNOWN)

            self.alert(self.__contacts)
            
        except:
            raise Exception("Error executing command " + cmdFormatted)